package com.th.supcom.pds.prescstatusquery.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.common.client.model.PdsSupplyProModel;
import com.th.supcom.pds.domain.vo.PdsSupplyResultSet;
import com.th.supcom.pds.internal.api.SupplyStatusQueryService;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.outdrugreturnapply.client.common.PdsSupplyResultModel;
import com.th.supcom.pds.prescstatusquery.client.service.SupplyStatusQueryRpc;

/**
 * 门诊退药控制层.
 * 
 * @author yzhang
 */
public class SupplyStatusQueryServiceController extends GwtController implements SupplyStatusQueryRpc
{
    private static final long serialVersionUID = 8165624832786257486L;
    private SupplyStatusQueryService m_supplyStatusQueryService;

    @Override
    public List <PdsSupplyResultModel> findSupplyStatusInfo (Map <String, Object> conditionMap) throws UiException
    {
        List <PdsSupplyResultModel> pdsSupplyResultModels = new ArrayList <PdsSupplyResultModel> ();
        List <PdsSupplyResultSet> pdsSupplyResults = m_supplyStatusQueryService.findSupplyStatusByCondition (conditionMap);
        if (pdsSupplyResults.isEmpty ())
        {
            s_logger.debug ("查询不到相关的退药信息！");
            throw new UiException ("查询不到相关的退药信息！");
        }
        for (int i = 0; i < pdsSupplyResults.size (); i++)
        {
            PdsSupplyResultModel pdsSupplyResultModel = new PdsSupplyResultModel ();
            pdsSupplyResultModel.setPdsSupplyModel ((PdsSupplyModel) ConvertUtils.convert (PdsSupplyModel.class, pdsSupplyResults.get (i).getPdsSupplyVO ()));
            List <PdsSupplyDetailModel> pdsSupplyDetailModels = ConvertUtils.convert (PdsSupplyDetailModel.class, pdsSupplyResults.get (i).getPdsSupplyDetailVOs ());
            pdsSupplyResultModel.setPdsSupplyDetailModels (pdsSupplyDetailModels);
            // 将pdsSupplyProVO 和pdsSupplyVO转化为 pdsSupplyProModel
            PdsSupplyProModel pdsSupplyProModel = (PdsSupplyProModel) ConvertUtils.convert (PdsSupplyProModel.class, pdsSupplyResults.get (i).getPdsSupplyProVO ());
            Map <String, Object> map = pdsSupplyProModel.getProperties ();
            pdsSupplyProModel = (PdsSupplyProModel) ConvertUtils.convert (PdsSupplyProModel.class, pdsSupplyResults.get (i).getPdsSupplyVO ());
            map.putAll (pdsSupplyProModel.getProperties ());
            pdsSupplyProModel.setProperties (map);
            pdsSupplyResultModel.setPdsSupplyProModel (pdsSupplyProModel);
            if (0 == i)
            {
                pdsSupplyResultModel.setPatientInfoModel ((PatientInfoModel) ConvertUtils.convert (PatientInfoModel.class, pdsSupplyResults.get (i).getPatientInfo ()));
            }
            pdsSupplyResultModels.add (pdsSupplyResultModel);
        }
        return pdsSupplyResultModels;
    }

    public void setSupplyStatusQueryService (SupplyStatusQueryService supplyStatusQueryService)
    {
        m_supplyStatusQueryService = supplyStatusQueryService;
    }

    /**
     * 日志记录器.
     */
    public static final Logger s_logger = LoggerFactory.getLogger (SupplyStatusQueryServiceController.class);

}